package com.co.common.util;

import java.util.Random;
import java.util.UUID;

/**
 * 随机生成验证码工具类
 *
 * @author ldl.plus
 */
public class ValidateCodeUtils {
    /**
     * 随机生成存数字类型验证码
     *
     * @param length 长度为4位或者6位
     * @return 纯数字验证码
     */
    public static String numCode(int length) {
        Random random = new Random();
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < length; i++) {
            code.append(random.nextInt(10));
        }
        return code.toString();
    }

    /**
     * 随机生成任意长度字符串验证码
     *
     * @param length 指定长度
     * @return 字母与数字验证码
     */
    public static String strCode(int length) {
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < (length / 32) + 1; i++) {
            code.append(UUID.randomUUID().toString().replace("-" , ""));
        }
        return code.substring(0, length);
    }

}
